using System.Windows.Controls;
using FluentSilverlight.Expressions;
using FluentSilverlight.MVVM;

namespace FluentSilverlight
{
    public abstract class OpinionatedContentControlBase<TModel> : 
        ContentControl,
        IOpinionatedControl<TModel> where TModel : class, IViewModel
    {
        private bool isLoaded;

        protected OpinionatedContentControlBase()
        {
            Loaded += (s, e) =>
                          {
                              if(isLoaded) return;

                              Model.Initialize();

                              isLoaded = true;
                          };
        }

        private TModel model;
        public TModel Model
        {
            get { return model; }
            set
            {
                model = value;
                DataContext = value;
            }
        }

        public void SetModel(IViewModel viewModel)
        {
            Model = (TModel)viewModel;
        }

        public abstract void Initialize();
    }
}